﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MAT.Model;

namespace MAT.Controllers
{
    class ctrRequestNewAsset
    {
        private frmRequestNewAsset _frmRequestNewAsset = null;

        public frmRequestNewAsset FrmRequestNewAsset
        {
            get { return _frmRequestNewAsset; }
            set { _frmRequestNewAsset = value; }
        }

        MAT_Context context = MATContext.getContext();

        private bool check()
        {
            if (_frmRequestNewAsset.txtReason.Text.TrimStart().Length < 1 || _frmRequestNewAsset.txtReason.Text.TrimStart().Length > 200)
            {
                MessageBox.Show("Your reason is from 1 to 200 digits");
                _frmRequestNewAsset.txtReason.Focus();
                return false;
            }
            else
                return true;
        }

        public void frmRequestNewAsset_Load()
        {
            try
            {
                _frmRequestNewAsset.cmbRNACategory.DataSource = context.CATEGORies.ToList();
                _frmRequestNewAsset.cmbRNACategory.DisplayMember = "CATEGORY_NAME";
                _frmRequestNewAsset.cmbRNACategory.ValueMember = "CATEGORY_ID";
                _frmRequestNewAsset.txtOwner.Text = MAT.Controllers.ctrLogin.Username;
                _frmRequestNewAsset.txtDate.Text = DateTime.Now.ToString();
            }
            catch { }
        }

        public void btnReset_Click()
        {
            _frmRequestNewAsset.txtReason.Text = "";
            _frmRequestNewAsset.cmbRNACategory.Text = "";
        }

        public void btnClose_Click()
        {
            _frmRequestNewAsset.Close();
            MAT.Controllers.ctrMain._frmMain.pnlMain.Visible = false;
        }

        public void btnSend_Click()
        {
            if (check())
            {
                REQUEST_NEW request_new = new REQUEST_NEW();
                request_new.EMPLOYEE_ID = MAT.Controllers.ctrLogin.employee_ID;
                request_new.CATEGORY_ID = _frmRequestNewAsset.cmbRNACategory.SelectedValue.ToString();
                request_new.REQUEST_DATE = DateTime.Now;
                request_new.REASON = _frmRequestNewAsset.txtReason.Text;
                if (MAT.Controllers.ctrLogin.priority == 3 || MAT.Controllers.ctrLogin.priority == 2)
                    request_new.REQ_STATUS = "RE001";
                else
                    request_new.REQ_STATUS = "RE003";
                try
                {
                    context.REQUEST_NEW.AddObject(request_new);
                    context.SaveChanges();
                    MessageBox.Show("SUCCESSFUL!");
                    btnReset_Click();
                }
                catch { }
            }
        }

    }
}
